package com.geek.controller;

import com.geek.bean.BaseRespVo;
import com.geek.bean.OrderCommentVo;
import com.geek.bean.PageData;
import com.geek.bean.data.OrderDetailData;
import com.geek.bean.data.RequestOrderSubmitData;
import com.geek.model.MarketOrderGoods;
import com.geek.service.OrderService;
import com.geek.service.SystemService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @description:
 * @author: yelinbo
 * @date: 2024/06/15 09:12
 **/
@RestController
@RequestMapping("wx/order")
public class    WxOrderController {
    @Autowired
    OrderService orderService;
    @Autowired
    SystemService systemService;
    @RequestMapping("submit")
    public BaseRespVo submit(@RequestBody RequestOrderSubmitData requestOrderSubmitData){
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()){
            Integer userId = (Integer) subject.getPrincipal();
            Map<String, String> systemConfig = systemService.toMap();
            int freightMin = Integer.parseInt(systemConfig.get("market_express_freight_min"));
            int freightValue = Integer.parseInt(systemConfig.get("market_express_freight_value"));
            Map<String,Object> data = orderService.submitOrderMessage(userId,requestOrderSubmitData,freightMin,freightValue);
            return BaseRespVo.ok(data);
        } else {
            return BaseRespVo.unAuthc();
        }
    }
    @GetMapping("list")
    public BaseRespVo list (Short showType,Integer page,Integer limit){
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            Integer userId = (Integer) subject.getPrincipal();
            PageData data = orderService.reListData(userId,showType,page,limit);
            return BaseRespVo.ok(data);
        }else {
            return BaseRespVo.unAuthc();
        }
    }

    @GetMapping("detail")
    public BaseRespVo detail (Integer orderId){
        OrderDetailData data = orderService.getOrderDetail(orderId);
        return BaseRespVo.ok(data);
    }
    @PostMapping("comment")
    public BaseRespVo comment(@RequestBody OrderCommentVo orderCommentVo){
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            Integer userId = (Integer) subject.getPrincipal();
            orderService.saveComment(orderCommentVo,userId);
            return BaseRespVo.ok(null);
        }else {
            return BaseRespVo.unAuthc();
        }
    }
    @PostMapping("cancel")
    public BaseRespVo cancel(@RequestBody Map map){
        orderService.cancelOrder(map.get("orderId"));
        return BaseRespVo.ok(null);
    }
    @PostMapping("refund")
    public BaseRespVo refund(@RequestBody Map map){
        orderService.updateRefund(map.get("orderId"));
        return BaseRespVo.ok(null);
    }
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody Map map){
        orderService.orderDelete(map.get("orderId"));
        return BaseRespVo.ok(null);
    }
    @PostMapping("confirm")
    public BaseRespVo confirm(@RequestBody Map map){
        orderService.orderConfirm(map.get("orderId"));
        return BaseRespVo.ok(null);
    }
    @GetMapping("goods")
    public BaseRespVo goods(Integer orderId,Integer goodsId){
        MarketOrderGoods data = orderService.orderGoodsByGoodsIdAndOrderId(orderId,goodsId);
        return BaseRespVo.ok(data);
    }
    @PostMapping("prepay")
    public BaseRespVo prepay(@RequestBody String orderId){
        return BaseRespVo.ok();
    }


}
